package com.baoxin.chenxixc4coach;

import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.baoxin.chenxixc4coach.fragments.IndexFragment;
import com.baoxin.chenxixc4coach.fragments.MyInfoFragment;
import com.baoxin.chenxixc4coach.fragments.AroundFragment;
import com.baoxin.chenxixc4coach.fragments.TeachingFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends CheckLocatePermissionsActivity implements TabLayout.OnTabSelectedListener {

    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;
    private Fragment currentFragment;
    private List<Fragment> fragments = new ArrayList<>();

    private IndexFragment indexFragment;
    private TeachingFragment teachingFragment;
    private AroundFragment aroundFragment;
    private MyInfoFragment myInfoFragment;

    private TabLayout tabLayout;
    public static boolean isRun = false;//标志主页是否启动过
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        isRun = true;
    }

    private void init() {

        tabLayout = findViewById(R.id.tab_main);
        tabLayout.addTab(tabLayout.newTab().setCustomView(tab_item("首页", R.drawable.selector_tab_index)));
        tabLayout.addTab(tabLayout.newTab().setCustomView(tab_item("教学", R.drawable.selector_tab_train)));
        tabLayout.addTab(tabLayout.newTab().setCustomView(tab_item("周边", R.drawable.selector_tab_student)));
        tabLayout.addTab(tabLayout.newTab().setCustomView(tab_item("我的", R.drawable.selector_tab_my)));

        tabLayout.setOnTabSelectedListener(this);

        fragmentManager = getSupportFragmentManager();
        indexFragment = new IndexFragment();
        teachingFragment = new TeachingFragment();
        aroundFragment = new AroundFragment();
        myInfoFragment = new MyInfoFragment();

        transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.root_view, indexFragment);
        fragments.add(indexFragment);
        transaction.show(indexFragment);
        currentFragment = indexFragment;
        transaction.commit();
    }

    private View tab_item(String name, int iconID) {
        View newtab = LayoutInflater.from(this).inflate(R.layout.tab_item_layout, null, false);
        TextView tv = newtab.findViewById(R.id.bottom_tab_text);
        tv.setText(name);
        ImageView iv = newtab.findViewById(R.id.bottom_tab_icon);
        iv.setImageResource(iconID);
        return newtab;
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        switch (tab.getPosition()) {
            case 0://首页
                switchFragment(indexFragment);
                break;
            case 1://教学
                switchFragment(teachingFragment);
                break;
            case 2://学员
                switchFragment(aroundFragment);
                break;
            case 3://我的
                switchFragment(myInfoFragment);
                break;
        }
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

    private void switchFragment(Fragment fragment) {
        transaction = fragmentManager.beginTransaction();
        if (!fragments.contains(fragment)) {
            fragments.add(fragment);
            transaction.add(R.id.root_view, fragment);
        }

        transaction.hide(currentFragment).show(fragment);
        currentFragment = fragment;
        transaction.commit();
    }


    //手动分发
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        //       super.onActivityResult(requestCode, resultCode, data);
        for (Fragment f : fragmentManager.getFragments()) {
            f.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        isRun = false;
    }
}
